package com.bootdo.program.wallpaper.controller;

import com.bootdo.common.config.Constant;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.program.wallpaper.domain.BzConfigDO;
import com.bootdo.program.wallpaper.domain.WxAdPageDO;
import com.bootdo.program.wallpaper.service.BzConfigService;
import com.bootdo.program.wallpaper.service.WxAdPageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 微信广告页面展示配置
 * 
 * @author guoyunfei
 * @email 2549789059@qq.com
 * @date 2021-08-04 22:12:08
 */
 
@Controller
@RequestMapping("/wallpaper/wxAdPage")
public class WxAdPageController  extends BaseController {
	@Autowired
	private WxAdPageService wxAdPageService;

	@Autowired
	private BzConfigService bzConfigService;
	
	@GetMapping()
	@RequiresPermissions("wallpaper:wxAdPage:wxAdPage")
	String WxAdPage(){
	    return "wallpaper/wxAdPage/wxAdPage";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wallpaper:wxAdPage:wxAdPage")
	public PageUtils list(@RequestParam Map<String, Object> params){
		params.put("appid",isAdmin() ? null : (null == defaultAppid() ? "noData" : defaultAppid()));
		//查询列表数据
        Query query = new Query(params);
		List<WxAdPageDO> wxAdPageList = wxAdPageService.list(query);
		int total = wxAdPageService.count(query);
		PageUtils pageUtils = new PageUtils(wxAdPageList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wallpaper:wxAdPage:add")
	String add(Model model){
		List<BzConfigDO> appletPage = bzConfigService.getType("applet_page");
		model.addAttribute("appletPage", appletPage);
		return "wallpaper/wxAdPage/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wallpaper:wxAdPage:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		WxAdPageDO wxAdPage = wxAdPageService.get(id);
		model.addAttribute("wxAdPage", wxAdPage);

		List<BzConfigDO> appletPage = bzConfigService.getType("applet_page");
		model.addAttribute("appletPage", appletPage);
		return "wallpaper/wxAdPage/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wallpaper:wxAdPage:add")
	public R save( WxAdPageDO wxAdPage){
		if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
			return R.error(1, "演示系统不允许修改,完整体验请部署程序");
		}
		if(null == defaultAppid()){
			return R.error("保存失败，请先配置小程序appid");
		}
		wxAdPage.setAppid(defaultAppid());
		if(wxAdPageService.save(wxAdPage)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wallpaper:wxAdPage:edit")
	public R update( WxAdPageDO wxAdPage){
		if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
			return R.error(1, "演示系统不允许修改,完整体验请部署程序");
		}
		if(null == defaultAppid()){
			return R.error("保存失败，请先配置小程序appid");
		}
		wxAdPage.setAppid(defaultAppid());
		wxAdPageService.update(wxAdPage);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wallpaper:wxAdPage:remove")
	public R remove( Integer id){
		if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
			return R.error(1, "演示系统不允许修改,完整体验请部署程序");
		}
		if(wxAdPageService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wallpaper:wxAdPage:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		wxAdPageService.batchRemove(ids);
		return R.ok();
	}
	
}
